//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // // ----------------------------- Ebrahim Foulaadvand, 08 July 2012 ------------------------------------- // // // // The programme "DampedOscillator" evaluates the temporal evolution of a damped harmonic oscillator // // for the three case of underdamped, overdamped and critically damped by three algorithms: // // Euler-Richardson (ER), Euler-Crommer (EC) and 2nd order Runge-Kutta (RK2). When you choose // // one of the algorithms, remeber to inactive the corresponding routines/files for the other ones. // // // // // // // //////////////////////////////////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include #include #include #include #include using namespace std; double a (double &x,double &v); // a is the accelration function double tau=0.01,T=30,ac,k=9.,m=1.,x0=0.,v0=1.,gamma=8.,E,Eudanal,Ecdanal,Eodanal,kx1,kx2,lx1,lx2,tmid, xmid,vmid,D,A,B,A1,A2,delta,omega0=sqrt(k/m),omega1,omega2,beta=gamma/(2*m); // m=oscillator mass,k=spring constant, gamma=dmping coefiicien, T= total simulation time, x0=initial position // v0=initial velocity, omega0=natural frequency, tau=timestep, E=total energy. int N=(T/tau); // N=number of timesteps main(){ vector v(N+1,0),x(N+1,0),xanal(N+1,0),vanal(N+1,0),Eud(N+1,0),Ecd(N+1,0),Eod(N+1,0); // Arrays x and v store the oscillator's position and velocity time step t. Arrays Eud,Ecd and Eod // store the oscillator total energy at each time step for the three cases of underdamped, // critically damped and overdamped. Arrays xanal and vanal show the anlytic solutions. ofstream filexER("xER.plt"); // output file for the position (ER algorithm) ofstream filevER("vER.plt"); // output file for the velocity (ER algorithm) ofstream filexEC("xEC.plt"); // output file for the position (EC algorithm) ofstream filevEC("vEC.plt"); // output file for the velocity (EC algorithm) ofstream filexRK2("xRK2.plt"); // output file for the position (RK2 algorithm) ofstream filevRK2("vRK2.plt"); // output file for the velocity (RK2 algorithm) //ofstream fileEudEC("EudEC gamma=5.plt"); //ofstream fileEcdEC("EcdEC gamma=6.plt"); ofstream fileEodEC("EodEC gamma=8.plt"); //ofstream fileEudanal("Eudanal gamma=5.plt"); //ofstream fileEcdanal("Ecdanal gamma=6.plt"); ofstream fileEodanal("Eodanal gamma=8.plt"); ofstream fileEnergyEC("EnergyEC.plt"); ofstream filephaseEC("PhaseSpaceEC.plt"); ofstream filexudanal("xudanal.plt"); ofstream filexcdanal("xcdanal.plt"); ofstream filexodanal("xodanal.plt"); // -------------------- initial conditions -------------------- x[0]=x0; v[0]=v0; // -------------------- under damped case ---------------------- //omega1=sqrt(omega0*omega0-beta*beta); //delta=-atan((v0/x0) + beta)/omega1; //D=x0/cos(delta); // -------------------- critically damped case ---------------------- //A=x0; B=v0+beta*x0; // -------------------- over damped case ---------------------- omega2=sqrt(beta*beta-omega0*omega0); A1=(x0*(omega2+beta)+v0)/(2*omega2); A2=(x0*(omega2-beta)-v0)/(2*omega2); //------------------------ Euler-Cromer Method ------------------------ for(int t=0;t